package org.example.word;

import com.alibaba.fastjson.JSONObject;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observables.GroupedObservable;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.example.word.model.CaseNotice;
import org.example.word.model.ProcessOpinion;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ReadWordDemo {
    public static final String FILE_PATH = "E:\\work_template\\办件通知单带附件.docx";
    public static void main(String[] args) {

        File workFile = new File(FILE_PATH);
        try (InputStream inputStream = new FileInputStream(workFile)) {
            XWPFDocument xwpf = new XWPFDocument (inputStream);
            List<XWPFTable> tables = xwpf.getTables();
            for (XWPFTable table : tables) {
                int rowCount = table.getNumberOfRows();

                for (int i = 0; i < rowCount; i++) {
                    XWPFTableRow row = table.getRow(i);
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (int j = 0; j < cells.size(); j++) {
                        System.err.println("第" + i + "行 第" + j + "列:内容“" + cells.get(j).getText() + "”");
                    }

                }
                /*readTableContent(table,rowCount);*/

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param table
     * @param rowCount
     */
    public static void readTableContent(final XWPFTable table, int rowCount){
        CaseNotice caseNotice = new CaseNotice();
        String title = table.getRow(1).getCell(1).getText();
        String content = table.getRow(2).getCell(1).getText();
        String name = table.getRow(3).getCell(1).getText();
        String telephone = table.getRow(4).getCell(1).getText();
        String mobile = table.getRow(5).getCell(1).getText();
        String address = table.getRow(6).getCell(1).getText();
        String email = table.getRow(7).getCell(1).getText();
        String postCode = table.getRow(8).getCell(1).getText();
        String idType = table.getRow(9).getCell(1).getText();
        String idNumber = table.getRow(10).getCell(1).getText();
        String sex = table.getRow(11).getCell(1).getText();
        String age = table.getRow(12).getCell(1).getText();
        String attachFile = table.getRow(13).getCell(1).getText();
        String category = table.getRow(14).getCell(1).getText();

        String target = table.getRow(15).getCell(1).getText();
        String isPublic = table.getRow(15).getCell(3).getText();

        String submitTime = table.getRow(16).getCell(1).getText();
        String serial = table.getRow(16).getCell(3).getText();

        String ip = table.getRow(17).getCell(1).getText();

        final List<ProcessOpinion> opinions = new ArrayList<>();
        Observable.range(20,(rowCount - 20))
                .buffer(3)
                .subscribe(new Consumer<List<Integer>>() {
                    @Override
                    public void accept(List<Integer> integers) throws Exception {
                        ProcessOpinion opinion = new ProcessOpinion();
                        for (Integer integer : integers) {
                            int result = integer % 3;
                            if (result == 2) {
                                String replyContent = table.getRow(integer).getCell(1).getText();
                                opinion.setReplyContent(replyContent);
                            }else if(result == 0){
                                String replyUnit = table.getRow(integer).getCell(1).getText();
                                String replyTime = table.getRow(integer).getCell(3).getText();
                                opinion.setReplyUnit(replyUnit);
                                opinion.setReplyTime(replyTime);
                            }else if (result == 1) {
                                String attachment = table.getRow(integer).getCell(1).getText();
                                opinion.setAttachment(attachment);
                            }

                        }
                        opinions.add(opinion);
                    }
                });
        caseNotice.setTitle(StringTrim(title));
        caseNotice.setContent(StringTrim(content));
        caseNotice.setName(StringTrim(name));
        caseNotice.setTelephone(StringTrim(telephone));
        caseNotice.setMobile(StringTrim(mobile));
        caseNotice.setAddress(StringTrim(address));
        caseNotice.setEmail(StringTrim(email));
        caseNotice.setPostCode(StringTrim(postCode));
        caseNotice.setIdType(StringTrim(idType));
        caseNotice.setIdNumber(StringTrim(idNumber));
        caseNotice.setSex(StringTrim(sex));
        caseNotice.setAge(StringTrim(age));
        caseNotice.setAttachFile(StringTrim(attachFile));
        caseNotice.setCategory(StringTrim(category));
        caseNotice.setTarget(StringTrim(target));
        caseNotice.setIsPublic(StringTrim(isPublic));
        caseNotice.setSubmitTime(StringTrim(submitTime));
        caseNotice.setSerial(StringTrim(serial));
        caseNotice.setIp(StringTrim(ip));
        System.err.println(JSONObject.toJSONString(caseNotice,true));
    }

    /**
     * 去空格
     * @param str
     * @return
     */
    private static  String StringTrim(String str){
        return str.replaceAll("[\\s\\u00A0]+","").trim();
    }
}
